package spacegame.resources;

import java.io.IOException;
import java.io.InputStream;

import org.andengine.entity.scene.background.RepeatingSpriteBackground;
import org.andengine.opengl.texture.TextureManager;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.atlas.bitmap.source.AssetBitmapTextureAtlasSource;
import org.andengine.opengl.texture.bitmap.BitmapTexture;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.TextureRegionFactory;
import org.andengine.opengl.texture.region.TiledTextureRegion;
import org.andengine.util.adt.io.in.IInputStreamOpener;

import spacegame.interfaces.ISpaceWorldContainer;
import android.content.Context;

public class ResourceFactory {

	
	protected ITextureRegion m_marsTextureRegion;
	protected ITextureRegion m_venusTextureRegion;
	protected ITextureRegion m_jupiterTextureRegion;
	
	protected ITextureRegion m_spaceshipTextureRegion;
	protected ITextureRegion m_onScreenControlBaseTextureRegion;
	protected ITextureRegion mOnScreenControlKnobTextureRegion;
	protected BitmapTextureAtlas m_onScreenControlTexture;
	private RepeatingSpriteBackground mRepeatingBackground;
	private TiledTextureRegion m_toggleButtonTextureRegion;
	private BitmapTextureAtlas mHUDTexture;
	private ITextureRegion m_sunTextureRegion;
	
	public enum PlanetType {
	    Mars, 
	    Venus, 
	    Jupiter, 
	    Sun
	}
	
	public enum TextureType {
		
	    ControlBaseTexture, 
	    ControlKnobTexture		
	}
	
	
	
	public ResourceFactory(ISpaceWorldContainer container, final int pWorldSize )
	{
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
		
		this.m_marsTextureRegion = loadTexture(container.GetContext(),
				"gfx/mars_planet.png", container.GetCurrentEngine().getTextureManager());
		this.m_venusTextureRegion = loadTexture(container.GetContext(),
				"gfx/venus_planet.png", container.GetCurrentEngine().getTextureManager());
		this.m_jupiterTextureRegion = loadTexture(container.GetContext(),
				"gfx/jupiter_planet.png", container.GetCurrentEngine().getTextureManager());
		this.m_spaceshipTextureRegion = loadTexture(container.GetContext(),
				"gfx/spaceship.png",container.GetCurrentEngine().getTextureManager());
		this.m_sunTextureRegion = loadTexture(container.GetContext(),
				"gfx/sun.png",container.GetCurrentEngine().getTextureManager());
		
		this.m_onScreenControlTexture = new BitmapTextureAtlas(
				container.GetCurrentEngine().getTextureManager(), 256, 128, TextureOptions.BILINEAR);
		this.m_onScreenControlBaseTextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(this.m_onScreenControlTexture, container.GetContext(),
						"onscreen_control_base.png", 0, 0);
		// this.mOnScreenControlBaseTextureRegion.setTextureSize(this.mOnScreenControlBaseTextureRegion.getWidth()*2,
		// this.mOnScreenControlBaseTextureRegion.getHeight()*2);
		this.mOnScreenControlKnobTextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(this.m_onScreenControlTexture, container.GetContext(),
						"onscreen_control_knob.png", 128, 0);
		this.m_onScreenControlTexture.load();
		
		mRepeatingBackground =new RepeatingSpriteBackground(
				container.GetCurrentCamera().getWidth() * (pWorldSize * 2 + 1),
				container.GetCurrentCamera().getHeight() * (pWorldSize * 2 + 1),
				container.GetCurrentEngine().getTextureManager(),
				AssetBitmapTextureAtlasSource.create(container.getAssets(),
						"gfx/backgroundstars.jpg"),
						container.GetCurrentEngine().getVertexBufferObjectManager());
		
		this.mHUDTexture = new BitmapTextureAtlas(container.GetCurrentEngine().getTextureManager(), 800, 32,TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.m_toggleButtonTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mHUDTexture, container.GetContext(), "engine_fire.png", 0, 0, 25, 1); // 256x128
		this.mHUDTexture.load();
	}
	
	
	public ITextureRegion GetShipTexture ()
	{
		return m_spaceshipTextureRegion;		
	}
	
	
	public ITextureRegion TryGetPlanetTexture (PlanetType item)
	{
		switch (item) {
		case Mars :
			return m_marsTextureRegion;
		case Venus :
			return m_venusTextureRegion;
		case Jupiter :
			return m_jupiterTextureRegion;
		case Sun :
			return m_sunTextureRegion;
		default:
			return null;
	}
		
	}
	
	public ITextureRegion TryGetTexture (TextureType item)
	{
		switch (item) {
		case ControlBaseTexture :
			return m_onScreenControlBaseTextureRegion;
		case ControlKnobTexture :
			return mOnScreenControlKnobTextureRegion;	
		default:
			return null;
	}
		
	}
	
	
	public RepeatingSpriteBackground GetRepeatingBackground() {
		return mRepeatingBackground;
	}

	
	public TiledTextureRegion GetFireSprite() {
		return m_toggleButtonTextureRegion;
	}

	
	//For load texture method
	private final ITextureRegion loadTexture(final Context pActivity,final String path, TextureManager textureManager ) {
		try {
			
			final BitmapTexture _bitmapTexture = new BitmapTexture(textureManager, new IInputStreamOpener() {
				
				@Override
				public InputStream open() throws IOException {
					return pActivity.getAssets().open(path);
				}
			});
			_bitmapTexture.load();
			final ITextureRegion _textureRegion = TextureRegionFactory.extractFromTexture(_bitmapTexture);
			return _textureRegion;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}





	
}
